package org.darkforest.common.util;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by valentina on 15/2/20.
 * 各种序列号/唯一编号生成器
 */
public class SerialNumGenerator implements Serializable {


    public static final String ORDERCH_PC = "1";               //pc,web网页端订单渠道
    public static final String ORDERCH_WEBCHART = "2";         //微信端订单渠道
    public static final String SG_ORDERCH_WEBCHART = "3";         //团购订单渠道

    public static final String ORDERCH_APP = "3";              //app端 估计用不上

    public static final String RECHARGE_WEBCHART = "4";              //微信端充值单号

    public static final String ORDER_BATCH_NUMBER_START = "8";

    /**
     * 订单序列号(24位)
     * 生成规则：1/2(订单渠道)+yyMMddHHmmssSSS+3位随机码
     *
     * @return
     */
    public static Long getOrderSn(String channel) {
        String time = DateUtil.dateToString(new Date(), DateUtil.COMPACT_TIME_PATTERN);
        //  Math.ceil(Math.random()*10) 返回1~10
        // Math.floor(Math.random()*10)返回0~9
        int random = (int) Math.ceil(Math.random() * 1000);
        while(random == 1000) {
            random = (int) Math.ceil(Math.random() * 1000);
        }
        // 补足19位， Long.parseLong最多19位
        //return Long.parseLong(channel + time + random);
        return Long.parseLong(channel + time +  String.format("%03d", random));
    }


    /**
     * 订单批次号 用于拆单 8开头
     * @return
     */
    public static Long getOrderBatchNum(){
        String time = DateUtil.dateToString(new Date(), DateUtil.COMPACT_TIME_PATTERN);
        int random = (int) Math.ceil(Math.random() * 10000000);
        return Long.parseLong(ORDER_BATCH_NUMBER_START  + random);
    }

    public static Long getSgOrderID(String channel){
        String time = DateUtil.dateToString(new Date(), DateUtil.COMPACT_TIME_PATTERN);
        int random = (int) Math.ceil(Math.random() * 1000);
        return Long.parseLong(channel + time + random);
    }


    public static void main(String[] args) {
        System.out.println(DateUtil.dateToString(new Date(), DateUtil.COMPACT_TIME_PATTERN));
        System.out.print(SerialNumGenerator.getOrderSn(ORDERCH_PC));
    }
}
